from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from page.net_project_page import NetProjectPage
from common.page import IOS, XPATH
from page.common_tools import CommonTools
from time import sleep
from selenium.common.exceptions import NoSuchElementException


class zizhuCourseTest(NetProjectPage, CommonTools):
    # 课程学习列表页
    stage_selected = (IOS, 'type == "XCUIElementTypeStaticText" AND label CONTAINS "|"')  # 选择阶段
    course_name = (IOS, 'type == "XCUIElementTypeStaticText" AND label CONTAINS "数学-必修"')
    to_view_btn = (IOS, 'type == "XCUIElementTypeButton" AND label == "去查看"')
    go_study = (IOS, 'type == "XCUIElementTypeImage" AND name == "auto_goStudy"')
    # 课程详情页面
    tab_1 = (IOS, 'type == "XCUIElementTypeButton" AND label == "简介"')
    tab_2 = (IOS, 'type == "XCUIElementTypeButton" AND label == "目录"')
    tab_3 = (IOS, 'type == "XCUIElementTypeButton" AND label == "笔记"')
    tab_4 = (IOS, 'type == "XCUIElementTypeButton" AND label == "评论"')
    # 简介
    start_btn = (IOS, 'type == "XCUIElementTypeButton" AND label == "开始学习"')
    continue_btn = (IOS, 'type == "XCUIElementTypeButton" AND label == "继续学习"')
    full_screen_btn = (IOS, 'type == "XCUIElementTypeButton" AND label == "icon 全屏"')  # 使用中心点方法点击两次
    quit_full_btn = (IOS, 'type == "XCUIElementTypeButton" AND label == "icon back"')  # 使用中心点方法点击两次
    learn_time = (IOS, 'type == "XCUIElementTypeStaticText" AND label CONTAINS "正在学习"')
    # 目录
    video_list = (IOS, 'type == "XCUIElementTypeScrollView"')  # 课程列表的框
    # 笔记
    add_note_btn = (IOS, 'type == "XCUIElementTypeImage" AND name == "添加笔记.png"')
    note_content = (IOS, 'type == "XCUIElementTypeTextView" AND value == "请输入内容"')
    submit_note_btn = (IOS, 'type == "XCUIElementTypeButton" AND label == "完成"')
    # 评论
    comment_field = (IOS, 'type == "XCUIElementTypeTextField" AND value == "发布评论"')
    please = (IOS,'type == "//XCUIElementTypeOther/XCUIElementTypeTextView"')
    release_comment_btn = (IOS, 'type == "XCUIElementTypeButton" AND label == "发布"')

    def xuan_ke(self):
        self.find_element(XPATH,'//*[@name="去选课"]').click()
        self.find_button("不可取消选择").click()
        self.find_element(XPATH,'//*[@name="提交"]').click()
        sleep(3)
    def learn_course(self, title='【数学-必修】'):
        sleep(1)

        #此部分代码仅在第一次使用
        self.find_button(string="去查看", contains_flag=1).click()
        sleep(6)
        self.find_button(string="common back dark icon",contains_flag=1).click()

        self.find_element(IOS, 'type == "XCUIElementTypeStaticText" AND label CONTAINS "%s"' % title).click()
        sleep(1)
        self.find_element(XPATH, '//*[@name="去学习"]').click()
        self.find_element(XPATH,'//*[@name="目录"]').click()  #目录     ------------------------
        self.get_frame_center_and_swipe(self.video_list)
        sleep(0.5)
        self.find_element(XPATH,'//*[@name="笔记"]').click()  #笔记 -----------------
        self.find_element(*self.add_note_btn).click()
        self.find_element(*self.note_content).send_keys('自从你生下来的那一刹那起，你就注定要回去。这中间的曲折磨难、顺畅欢乐便是你的命运。 命运总是与你一同存就会发觉，命运有一半在你手里，只有另一半才在上帝的手里。你一生的全部就在于：运用你手里所拥有的去获取上帝')
        self.find_element(*self.submit_note_btn).click()
        sleep(1)
        self.swipe_dr('down')
        self.find_static_text(string="评论",contains_flag=1).click()

    #不同机型需要改动 6s
        self.swipe(30,698,30,698)  #回复
        # self.find_element(XPATH,'//XCUIElementTypeTextField[contains(@vaule,"发布评论"]').click()
        # self.find_element(XPATH,'//*[@name="请输入内容"]').click()
        self.find_element(XPATH,'//XCUIElementTypeOther/XCUIElementTypeTextView').send_keys('不能设想，古罗马的角斗场需要重建，庞贝古城需要重建，柬埔寨的吴哥窟需要重建，玛雅文化遗址需要重建。这就像不能设想，远年的古铜器需要抛光，出土的断戟需要镀镍，宋版图书需要上塑，马王堆的汉代老太需要植皮丰胸，重施浓妆。')
        self.find_button(string="发布",contains_flag=1).click()
        sleep(4)
        self.find_element(XPATH,"//XCUIElementTypeStaticText[contains(@label,'不能设想，古罗马的角斗')]").click()     #二级回复
        sleep(1)
        self.swipe(20,696,20,696)
        self.find_element(XPATH,'//XCUIElementTypeOther/XCUIElementTypeTextView').send_keys('一个人可以不信神，但不可以不相信神圣。是否相信上帝、佛、真主或别的什么主宰宇宙的神秘力量，往往取决于个人所隶属的民族传统、文化背景和个人的特殊经历，人世间有任何神圣价值，挽救他的人生的彻底 失败。')
        self.find_button(string="发布",contains_flag=1).click()
        sleep(2)
        self.find_button(string="返回", contains_flag=1).click()
        try:
            # self.find_element(*self.start_btn, times=2).click()
            self.get_frame_center_and_click(self.start_btn)
        except NoSuchElementException:
            # self.find_element(*self.continue_btn, times=2).click()
            self.get_frame_center_and_click(self.continue_btn)
        sleep(6)
        self.find_button(string="common back dark icon", contains_flag=1).click()
        self.find_button(string="common back dark icon", contains_flag=1).click()
        self.find_button(string="common back dark icon", contains_flag=1).click()
        self.find_button(string="common back light icon",contains_flag=1).click()

    #  苹果6
        # self.swipe(20, 629, 20, 629)  # 发布评论
        # # self.find_text_view(string="请输入内容",contains_flag=1).send_keys('不是很好嘛，你说勒。。。。。')
        # self.find_element(*self.please).send_keys('不是很好嘛，你说嘞。。。')
        # sleep(0.5)
        # self.find_element(*self.release_comment_btn).click()
        # self.find_button(string="返回", contains_flag=1).click()
        # self.find_button(string="返回", contains_flag=1).click()
        # self.find_button(string="返回", contains_flag=1).click()
        # self.find_button(string="返回", contains_flag=1).click()


if __name__ == '__main__':
    test = zizhuCourseTest()
    test.enter_project()
    test.enter_tools('课程学习')
    test.learn_course()
